package com.margo.project.append.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.margo.project.append.config.LcamConfig;

@Component
public class LcamGenerateHelper {
	
	@Autowired
	LcamConfig lcamConfig;
	
	public String initFiledName(String columnName) {
		StringBuilder column=new StringBuilder(columnName);
		StringBuilder filedName=new StringBuilder();
		int j=-1;
		for(int i=0;i<column.length();i++) {
			if(column.substring(i, i+1).equals("_")) {
				j=i+1;
			}else {
				if(j==i) {
					filedName.append(column.charAt(i));
				}else {
					filedName.append(columnName.toLowerCase().charAt(i));
				}
				
			}
			
		}
		return filedName.toString();
	}

	
	public void fillFile(String pathName,String fileName,StringBuilder sb) {
		String filePath=lcamConfig.getFileLocation()+pathName+"\\";
		
		File path=new File(filePath);
		if(!path.exists()) {
			path.mkdirs();
		}
		Writer out=null;
		BufferedWriter bw=null;
		try {
			File file=new File(filePath+fileName+".java");
			bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "gbk"));
			bw.write(sb.toString());
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(out !=null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(bw !=null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
